<template>
  <hi-row
    isColumn
    :wrap="false"
    class="viewcontainer"
    v-if="dataset && subDataSet"
  >
    <hi-col flex="300px">
      <hi-toolbar
        :source="dataset"
        :addOne="true"
        :disabled="dataset.isReadonly()"
        v-bind="toolbar"
        ref="dbtoolbar"
      ></hi-toolbar>
      <btn-toolbar @callFunc="clickBtn" class="btn-toolbar"></btn-toolbar>
      <print
        :form-data="dataset.data[0]"
        :table-data="subDataSet.data"
        ref="print"
        v-if="dataset.data[0] && subDataSet.data"
      ></print>
      <hi-form
        :dataSource="dataset"
        ref="detailForm"
        :model="dataset.curRecord"
        v-bind="form"
        :disabled="dataset.isReadonly()"
      ></hi-form>
    </hi-col>
    <hi-col flex="auto" :min-height="200">
      <hi-table
        ref="detailTable"
        :dataSource="subDataSet"
        v-bind="dataTable"
        :model="subDataSet.data"
        :isEdit="!subDataSet.isReadonly()"
      ></hi-table>
    </hi-col>
  </hi-row>
</template>

<script>
import Print from "./print";
import BtnToolbar from "@/views/component/toolbar/index.vue";
import { HiForm, HiToolbar, HiNumber, HiTable, HiDataSelect } from "hi-ui";
import viewBase from "../../base";
import toolbar from "@/hiuiDefaultConfig/toolbar";
import adjustmentlCfg from "./dataset/adjustment";
import adjustmentDetailCfg from "./dataset/detail";
import audittag from "@/views/component/audittag";
import storeConfig from "../../store/query/dataset";
import { dataHelper } from "hi-ui";
import bind from "lodash/bind";
export default {
  name: "adjustdetail",
  extends: viewBase,
  components: {
    HiForm,
    HiToolbar,
    HiTable,
    HiNumber,
    audittag,
    Print,
    BtnToolbar,
  },

  data() {
    let me = this;
    return {
      //数据集
      dataset: null,
      //数据集配置
      datasetCfg: adjustmentlCfg,
      //从表数据集
      subDataSet: null,
      //从表数据集配置
      subDatasetCfg: adjustmentDetailCfg,

      //数据桥梁
      dataBridge: new Map(),
      //数据表格
      dataTable: {
        isEdit: true,
        hasSelection: true,
        hasRowNumber: true,

        columns: [
          {
            name: "FGOODSNUMBER",
            width: 160,
            element: HiDataSelect,
            props: {
              lookup: {
                where: {
                  criterionData: {
                    join: "and",
                    items: [
                      {
                        sign: "gt",
                        name: "FAMOUNT",
                        tablefilter: false,
                        dataType: "dbString",
                        value: "0",
                        enabled: true,
                      },
                      {
                        sign: "eq",
                        name: "FSHOPDESCID",
                        tablefilter: false,
                        dataType: "dbString",
                        value: bind(function () {
                          if (
                            this.dataset.data.length == 0 ||
                            !this.dataset.data[0].FSHOPDESCID
                          )
                            throw new Error("请选择店铺");
                          return this.dataset.data[0].FSHOPDESCID;
                        }, me),
                        enabled: true,
                      },
                    ],
                  },
                  join: "and",
                },
                storeCfg: storeConfig,
                active: true,
                multiSelect: false,
                addRecord: true,
                checkValid: true,
                target: {
                  id: "adjustmentDetailEntity",
                }, //目标
                source: {
                  id: "QueryEntity", //库存的数据
                  modelFile: "store/query/dataset/QueryEntity.xml",
                }, //来源 //来源数据集查询条件
                /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
                limitEqualsFields: [], //相同字段
                uniqueFields: [], //唯一字段
                returnFields: {
                  FGOODSNUMBER: "FGOODSNUMBER",
                  FBARCODE: "FBARCODE",
                  FDESC: "FDESC",
                  FBRAND: "FBRAND",
                  FSPEC: "FSPEC",
                  FUNIT: "FUNIT",
                  FPRICE: "FPRICE",
                  FVIPPRICE: "FVIPPRICE",
                  FDRUGCLASS: "FDRUGCLASS",
                  FCLASS: "FCLASS",
                  FPROADDRESS: "FPROADDRESS",
                  FDOSAGEFORM: "FDOSAGEFORM",
                  FABC: "FABC",
                  FMEDICARETYPE: "FMEDICARETYPE",
                  FAPPROVALNUMBER: "FAPPROVALNUMBER",
                  FAPPROVALDATE: "FAPPROVALDATE",
                },
                columns: [
                  {
                    fixed: true,
                    name: "FDESC",
                    label: "通用名称",
                  },

                  {
                    name: "FGOODSNUMBER",
                    label: "店内编码",
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                  },
                  // {
                  //   name: "FDRUGCLASS",
                  //   label: "药品分类",
                  // },
                  // {
                  //   name: "FCLASS",
                  //   label: "类别",
                  // },
                  {
                    name: "FBRAND",
                    label: "品牌",
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                  },
                  {
                    name: "FUNIT",
                    label: "单位",
                  },
                  {
                    name: "FPRICE",
                    label: "零售价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                      "controls-position": "right",
                    },
                  },
                  {
                    name: "FVIPPRICE",
                    label: "会员价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                      "controls-position": "right",
                    },
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                    {
                      name: "FGOODSNUMBER",
                      label: "店内编码",
                      element: "el-input",
                    },
                    {
                      name: "FBARCODE",
                      label: "条形码",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            name: "FBARCODE",
            width: 160,
            element: HiDataSelect,
            props: {
              lookup: {
                where: {
                  criterionData: {
                    join: "and",
                    items: [
                      {
                        sign: "gt",
                        name: "FAMOUNT",
                        tablefilter: false,
                        dataType: "dbString",
                        value: "0",
                        enabled: true,
                      },
                      {
                        sign: "eq",
                        name: "FSHOPDESCID",
                        tablefilter: false,
                        dataType: "dbString",
                        value: bind(function () {
                          if (
                            this.dataset.data.length == 0 ||
                            !this.dataset.data[0].FSHOPDESCID
                          )
                            throw new Error("请选择店铺");
                          return this.dataset.data[0].FSHOPDESCID;
                        }, me),
                        enabled: true,
                      },
                    ],
                  },
                  join: "and",
                },
                storeCfg: storeConfig,
                active: true,
                multiSelect: false,
                addRecord: true,
                checkValid: true,
                target: {
                  id: "adjustmentDetailEntity",
                }, //目标
                source: {
                  id: "QueryEntity", //库存的数据
                  modelFile: "store/query/dataset/QueryEntity.xml",
                }, //来源 //来源数据集查询条件
                /*   queryCdions: {
              FNUMBER: function (rec) {
                return rec["FNUMBER1"];
              },
            },  */ updateFields: [], //更新字段
                limitEqualsFields: [], //相同字段
                uniqueFields: [], //唯一字段
                returnFields: {
                  FGOODSNUMBER: "FGOODSNUMBER",
                  FBARCODE: "FBARCODE",
                  FDESC: "FDESC",
                  FBRAND: "FBRAND",
                  FSPEC: "FSPEC",
                  FUNIT: "FUNIT",
                  FPRICE: "FPRICE",
                  FVIPPRICE: "FVIPPRICE",
                  FDRUGCLASS: "FDRUGCLASS",
                  FCLASS: "FCLASS",
                  FPROADDRESS: "FPROADDRESS",
                  FDOSAGEFORM: "FDOSAGEFORM",
                  FABC: "FABC",
                  FMEDICARETYPE: "FMEDICARETYPE",
                  FAPPROVALNUMBER: "FAPPROVALNUMBER",
                  FAPPROVALDATE: "FAPPROVALDATE",
                },
                columns: [
                  {
                    fixed: true,
                    name: "FDESC",
                    label: "通用名称",
                  },

                  {
                    name: "FGOODSNUMBER",
                    label: "店内编码",
                  },
                  {
                    name: "FBARCODE",
                    label: "条形码",
                  },
                  // {
                  //   name: "FDRUGCLASS",
                  //   label: "药品分类",
                  // },
                  // {
                  //   name: "FCLASS",
                  //   label: "类别",
                  // },
                  {
                    name: "FBRAND",
                    label: "品牌",
                  },
                  {
                    name: "FSPEC",
                    label: "规格",
                  },
                  {
                    name: "FUNIT",
                    label: "单位",
                  },
                  {
                    name: "FPRICE",
                    label: "零售价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                      "controls-position": "right",
                    },
                  },
                  {
                    name: "FVIPPRICE",
                    label: "会员价",
                    formType: "dbDouble",
                    props: {
                      precision: 2,
                      format: "￥0.00",
                      "controls-position": "right",
                    },
                  },
                ],
                queryOption: {
                  fields: [
                    {
                      name: "FDESC",
                      label: "通用名称",
                      element: "el-input",
                    },
                    {
                      name: "FGOODSNUMBER",
                      label: "店内编码",
                      element: "el-input",
                    },
                    {
                      name: "FBARCODE",
                      label: "条形码",
                      element: "el-input",
                    },
                  ],
                },
              },
            },
          },
          {
            name: "FDESC",
          },
          // {
          //   name: "FDRUGCLASS",
          // },
          // {
          //   name: "FCLASS",
          // },
          {
            name: "FBRAND",
          },
          {
            name: "FSPEC",
          },
          {
            name: "FUNIT",
          },

          {
            label: "原零售价",
            name: "FPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FNEWPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },

          {
            label: "原会员价",
            name: "FVIPPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FNEWVIPPRICE",
            props: {
              min: 0,
              format: "￥0.00",
              precision: 2,
            },
          },

          {
            name: "FNOTE",
          },
        ],
      },
      toolbar: toolbar,
      //表单配置
      form: {
        columnsTotal: 2,
        items: [
          {
            name: "FNUMBER",
          },
          {
            name: "FSHOPDESC",
            element: "hi-select-grid",
            required: true,
            props: {
              selectOption: {
                loading: true,
                multiple: false,
                clearable: true,
                filterable: true,
                "allow-create": true,
                "collapse-tags": true,
              },
              lookup: {
                modelFile: "database/shop/dataset/ShopEntity.xml",
                label: "FDESC",

                tableprops: {
                  "show-header": false,
                  "max-height": 400,
                },
                returnFields: {
                  FSHOPDESC: "FDESC",
                  FSHOPDESCID: "FNUMBER",
                },
                columns: [
                  {
                    name: "FDESC",
                    label: "通用名称",
                  },
                ],
              },
            },
          },
          {
            name: "FNOTE",
            colspan: 2,
            props: {
              type: "textarea",
              autosize: { minRows: 4, maxRows: 4 },
              maxlength: 200,
            },
          },
          {
            name: "FUSERID",
          },
          {
            name: "FCREATEDATE",
          },
          {
            element: audittag,
            name: "FAUDIT",
          },
        ],
      },
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    clickBtn(methodName) {
      switch (methodName) {
        case "print":
          this.$refs.print.onPrint();
          break;
      }
    },
    addDetail() {
      debugger;
      this.subDataSet.add({});
    },
  },
  mounted() {
    //创建主数据集
    this.dataset = this.createDataSet(this.datasetCfg);
    this.dataset.on("dirtychange", (store, dirty) => {
      console.log("dataset" + dirty);
    });
    this.dataset.on("beforesave", (store) => {
      let shopDesc = store.data[0].FSHOPDESC,
        shopId = store.data[0].FSHOPDESCID;
      let subStore = store.models[0].getChildStore("adjustmentDetailEntity"),
        len = subStore.data.length;
      for (let i = 0; i < len; i++) {
        let item = subStore.data[i];
        item.FSHOPDESC = shopDesc;
        item.FSHOPDESCID = shopId;
      }
    });
    //创建从数据集
    this.subDataSet = this.createDataSet(this.subDatasetCfg, this.dataset);

    //设置数据与组件的关系
    this.dataBridge.set(this.dataset, ["detailForm", "detailTable"]);

    this.flowConfig = { dataset: this.dataset };
    if (this.$route.query.id) {
      this.dataset.load({
        FID: this.$route.query.id,
      });
    }
    console.log("表格-------------", this.subDataSet);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
</style>

